package cn.itsource.pethome.system.employee.service.imp;

import cn.itsource.pethome.base.service.impl.BaseServiceImpl;
import cn.itsource.pethome.constants.BaseConstants;
import cn.itsource.pethome.system.employee.domain.Employee;
import cn.itsource.pethome.system.employee.domain.EmployeeBo;
import cn.itsource.pethome.system.employee.mapper.EmployeeMapper;
import cn.itsource.pethome.system.employee.service.IEmployeeService;
import cn.itsource.pethome.system.logininfo.domain.Logininfo;
import cn.itsource.pethome.system.logininfo.mapper.LogininfoMapper;
import cn.itsource.pethome.system.role.mapper.RoleMapper;
import cn.itsource.pethome.utils.MD5Utils;
import cn.itsource.pethome.utils.StrUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author:R
 * @Date:2023/1/2 19:10
 * Description
 */
@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements IEmployeeService {
    @Resource
    private EmployeeMapper employeeMapper;
    @Resource
    private LogininfoMapper logininfoMapper;

    @Override
    public List<Employee> findAllBo() {
        List<EmployeeBo> all = employeeMapper.findAllBo();
        List<Employee> employees = new ArrayList<>();
        for (EmployeeBo one : all) {
            Employee employee = new Employee();
            //将原始对象中的值复制到新对象中
            BeanUtils.copyProperties(one,employee);
            employees.add(employee);
        }
        return employees;
    }

    @Override
    public void add(Employee employee) {
        super.add(employee);
        //保存角色
        employeeMapper.addEmpRole(employee.getId(),employee.getRole_id());
        //同步loginInfo
        String salt = StrUtils.getComplexRandomString(32);
        employee.setSalt(salt);
        employee.setPassword(MD5Utils.encrypByMd5(employee.getPassword()+salt));
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(employee,logininfo);
        logininfo.setType(BaseConstants.Login.TYPE_0);//设置账户类型为管理员
        logininfo.setDisable(BaseConstants.Login.DISABLE_1);//系统录入员工信息，直接可用
        logininfoMapper.add(logininfo);
        employee.setLogininfo_id(logininfo.getId());
        employeeMapper.update(employee);   //更新员工表
    }

    @Override
    public void update(Employee employee) {
        super.update(employee);
        //更新角色，先删除旧的，在添加新的
        employeeMapper.delRoleByEmpId(employee.getId());
        employeeMapper.addEmpRole(employee.getId(),employee.getRole_id());
        //同步loginInfo
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(employee,logininfo);
        logininfoMapper.update(logininfo);
    }

    @Override
    public void delete(Long id) {
        //先删除中间表
        employeeMapper.delRoleByEmpId(id);
        super.delete(id);
        //删除登录表
        Employee one = employeeMapper.findOne(id);
        Long id1 = logininfoMapper.findOne(one.getLogininfo_id()).getId();
        logininfoMapper.delete(id1);
    }
}
